package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.Constast;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.common.TreeNode;
import com.xjh.t.domain.Permission;
import com.xjh.t.service.IPermissionService;
import com.xjh.t.vo.PermissionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-24
 */
@RestController
@RequestMapping("permission")
public class PermissionController {


    @Autowired
    private IPermissionService permissionService;

    //权限管理开始
    @RequestMapping("loadPermissionManagerLeftJson")
    public DataGridView loadPermissionManagerLeftJson(PermissionVo vo)
    {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constast.TYPE_MENU);
        List<Permission> list = this.permissionService.list(queryWrapper);
        List<TreeNode> treeNodes = new ArrayList<>();
        for(Permission permission:list)
        {
            treeNodes.add(new TreeNode(permission.getId(),permission.getPid(),permission.getTitle(),permission.getOpen() == Constast.OPEN_TRUE));
        }

        return new DataGridView(treeNodes);
    }

    @RequestMapping("loadAllPermission")
    public DataGridView loadAllPermission(PermissionVo vo)
    {
        IPage<Permission> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("type",Constast.TYPE_PERMISSION);
        queryWrapper.like(StringUtils.isNotBlank(vo.getTitle()),"title",vo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(vo.getPercode()),"percode",vo.getPercode());

        queryWrapper.eq(vo.getId() != null,"pid",vo.getId());
        queryWrapper.orderByAsc("ordernum");
        this.permissionService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("addPermission")
    public ResultObj addPermission(PermissionVo vo)
    {
        try
        {
            vo.setType(Constast.TYPE_PERMISSION);
            this.permissionService.save(vo);
            return ResultObj.ADD_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("loadPermissionMaxOrderNum")
    public Map<String,Object> loadPermissionMaxOrderNum()
    {
        Map<String,Object> map = new HashMap<String,Object>();
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<Permission> list = this.permissionService.list(queryWrapper);
        if(list.size() > 0)
        {
            map.put("value",list.get(0).getOrdernum() + 1);
        }
        else
            map.put("value",1);
        return map;
    }

    @RequestMapping("updatePermission")
    public ResultObj updatePermission(PermissionVo vo)
    {
        try
        {
            this.permissionService.updateById(vo);
            return ResultObj.UPDATE_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.UPDATE_ERROR;
        }

    }

    @RequestMapping("deletePermission")
    public ResultObj deletePermission(Integer id)
    {
        try
        {
            this.permissionService.removeById(id);
            return ResultObj.DELETE_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.DELETE_ERROR;
        }

    }
    //权限管理结束
}

